// #include "log.hpp"
#include "Reactor.hpp"
#include "listener.hpp"
#include "HandleConnection.hpp"
#include "service.hpp"

#include <iostream>
#include <memory>

// using namespace log_namespace; 

int main(int argc, char *argv[])
{
    // if (argc != 2)
    // {
    //     std::cerr << "Usage: " << argv[0] << " local-port" << std::endl;
    //     exit(0);
    // }
    // uint16_t port = std::stoi(argv[1]);
    uint16_t port = std::stoi("8888");
    // EnableScreen();
    inetAddr localaddr("0.0.0.0", port);

    
	if (!MyLogger::instance()->init("log.conf")) {
		fprintf(stderr, "init log module failed.\n");
		return -1;
	}

    // 只处理新链接的到来的接口
    Listener listener(port);

    IOService parse;

    // 只处理普通套接字的接口
    HandleConnection handlemet(std::bind(&IOService::IOexecute, &parse, std::placeholders::_1));

    // 下面是 Reactor 做事件派发的
    std::unique_ptr<Reactor> R = std::make_unique<Reactor>();
    
    // 下面两句话都只是把bind后的 产生的可调用对象传给反应堆的成员
    // 但是这两句话中的可调用对象没有进行实例化
    R->set_OnConnect(std::bind(&Listener::accepter, &listener, std::placeholders::_1));
    R->set_OnNormalHandler(
        std::bind(&HandleConnection::HandleRecvr, &handlemet, std::placeholders::_1),
        std::bind(&HandleConnection::HandleSender, &handlemet, std::placeholders::_1),
        std::bind(&HandleConnection::HandleExceptr, &handlemet, std::placeholders::_1)
    );

    // 下面这句话给bind的accepter这个函数传递了实例化后的myconnection* conn对象
    R->AddConnection(listener.listensockfd(), EPOLLIN | EPOLLET, localaddr, ListenConnection);

    R->Dispatch();

	log4cpp::Category::shutdown();

    return 0;
}
